import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
} from "@angular/core";
import { ModalDirective } from "ngx-bootstrap";
import {
  ComboboxItemDto,
  MainTaskServiceProxy,
  TaskDto,
  SideTaskServiceProxy,
  CreateTaskDto,
  //DevelopmentStage
} from "@shared/service-proxies/service-proxies";
import { AppComponentBase } from "@shared/common/app-component-base";

import * as moment from "moment";
import { finalize } from "rxjs/operators";

@Component({
  selector: "createOrEditMaintaskModal",
  templateUrl: "./create-or-edit-maintask-modal.component.html",
})
export class CreateOrEditMainTaskComponent extends AppComponentBase {
  @ViewChild("createOrEditModal") modal: ModalDirective;
  @ViewChild("dueDatePicker") dueDatePicker: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;
  dueDate = new Date();
  order: TaskDto = new TaskDto();
  createTaskDto: CreateTaskDto = new CreateTaskDto();
  orderCategorys: ComboboxItemDto[];
  statuses: ComboboxItemDto[];
  type: number;
  // stages = DevelopmentStage;

  constructor(
    injector: Injector,
    private _mainTaskServiceProxy: MainTaskServiceProxy,
    private _sideTaskServiceProxy: SideTaskServiceProxy
  ) {
    super(injector);
  }

  show(id?: number): void {
    this.active = true;
    //this.type = type;
    //console.log(id);
    this.order = new TaskDto();
    this.createTaskDto = new CreateTaskDto();
    // this.statuses = DevelopmentStage;
    //let stages = DevelopmentStage;

    this.modal.show();
  }

  save(): void {
    this.saving = true;
    // this.order.receptionTime = moment().startOf('day');
    // this.order.completionTime = moment(this.dueDate);
    this.createTaskDto.producerId = 51;
    this.createTaskDto.dueDate = moment().startOf("day");
    //  this.createTaskDto.stage = this.stages.D;
    this._mainTaskServiceProxy
      .createMainTask(this.createTaskDto)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe(() => {
        this.notify.info(this.l("SavedSuccessfully"));
        this.close();
        this.modalSave.emit(null);
      });
    console.log(this.createTaskDto);
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }
}
